package com.meilai.project.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.controller.mini.vo.MiniRightTreeVO;
import com.meilai.project.entity.system.MiniRight;
import com.meilai.project.mapper.system.MiniRightMapper;
import com.meilai.project.service.system.MiniRightService;
import com.meilai.project.util.ThreadLocalUserUtil;
import com.meilai.project.util.TreeUtil;
import com.meilai.project.vo.TreeVO;
import com.meilai.project.vo.system.RightVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 张驰
 * @date 2020/11/16 10:48
 */
@Service
public class MiniRightServiceImpl extends ServiceImpl<MiniRightMapper, MiniRight> implements MiniRightService {

    @Override
    public List<TreeVO> getRights() {
        List<TreeVO> returnList = new ArrayList<>();
        List<MiniRight> rights = getRightsList();
        if(rights.size() <=0 ) return returnList;
        return getTreeVOByRouters(rights);
    }

    @Override
    public List<TreeVO> getRouterRights() {
        List<TreeVO> returnList = new ArrayList<>();
        List<MiniRight> rights = getRightsList(true);
        if(rights.size() <=0 ) return returnList;
        return getTreeVOByRouters(rights);
    }

    private List<MiniRight> getRightsList(Boolean ... canBeIndex){
        List<MiniRight> rights = new ArrayList<>();
        if(canBeIndex != null && canBeIndex.length == 1 && canBeIndex[0]) {
            rights =list(new QueryWrapper<MiniRight>().isNull("deleted_at").eq("can_be_index",1).orderByAsc("sort","pid","id"));
        }else{
            rights =list(new QueryWrapper<MiniRight>().isNull("deleted_at").orderByAsc("sort","pid","id"));
        }
        return rights!=null?rights:new ArrayList<>();
    }

    private List<TreeVO> getTreeVOByRouters(List<MiniRight> sourceRights){

        List<Long> rightVOIds = sourceRights.stream().map(MiniRight::getId).collect(Collectors.toList());

        List<RightVO> rights = baseMapper.getMiniRightVOs(rightVOIds);

        // 分组
        Map<Long, List<TreeVO>> collect = rights.stream().filter(item -> item.getStatus()==1).map(item -> {
            TreeVO vo = new TreeVO();
            BeanUtils.copyProperties(item,vo);
            vo.setLabel(item.getTitle());
            return vo;
        }).collect(Collectors.groupingBy(TreeVO::getPid));

        // pid=0的顶级菜单
        List<TreeVO> returnList = new ArrayList<>(collect.get(0L));
        returnList.forEach(root -> {
            TreeUtil.TreeForEachUseSort(collect,root);
        });

        return returnList;
    }

    @Override
    public List<MiniRightTreeVO> getMyRights() {
        List<MiniRightTreeVO> result = new ArrayList<>();

        Long userId = ThreadLocalUserUtil.get().getId();

        List<MiniRight> rights = baseMapper.getMiniRightsByUserId(userId);
        if(CollectionUtils.isEmpty(rights)) return result;

        // 分组
        Map<Long, List<MiniRightTreeVO>> collect = rights.stream().map(d -> {
            MiniRightTreeVO vo = new MiniRightTreeVO();
            BeanUtils.copyProperties(d, vo);
            return vo;
        }).collect(Collectors.groupingBy(MiniRightTreeVO::getPid));

        // pid=0的顶级菜单
        result = new ArrayList<>(collect.get(0L));
        result.forEach(root -> TreeUtil.TreeForEachUseSort(collect,root));
        result.sort(Comparator.comparingInt(MiniRightTreeVO::getSort));

        return result;
    }
}
